package com.hyj.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyj.service.UserchecksService;
import com.hyj.service.UsersService;
import com.hyj.mapper.AskersMapper;
import com.hyj.mapper.RolesMapper;
import com.hyj.mapper.UserRolesMapper;
import com.hyj.mapper.UserchecksMapper;
import com.hyj.entity.Askers;
import com.hyj.entity.Fenye;
import com.hyj.entity.Roles;
import com.hyj.entity.UserRoles;
import com.hyj.entity.Userchecks;
import com.hyj.entity.Users;

@Service
public class UserchecksServiceImp implements UserchecksService {
	@Autowired
	private UserchecksMapper userchecksmapper;
	@Autowired
	private AskersMapper askersmapper;
	@Autowired
	private UserRolesMapper userRolesmapper;
	@Autowired
	private RolesMapper rolesmapper;

	@Autowired
	private UsersService usersService;

	// 添加签到信息
	@Override
public Integer addUsercheck(HttpServletRequest request) {
		
		Userchecks userchecks=new Userchecks();
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		Users u = new Users();
		u.setLoginName(name);
		/* 根据名称查询用户信息 */
		UserRoles userRoles = new UserRoles();
		
		
		List<Users> selectuser = usersService.selectByname(u);
		userRoles.setUserId(selectuser.get(0).getId());
		List<UserRoles> selectURByRidAndUid = userRolesmapper.selectURByRidAndUid(userRoles);
		List<Integer> urlist = new ArrayList<Integer>();
		for (int i = 0; i < selectURByRidAndUid.size(); i++) {
				urlist.add(selectURByRidAndUid.get(i).getRoleId());
		}
		
		List<Roles> selectRoles = rolesmapper.selectRoles(urlist);
		
		Integer k=0;
		List<UserRoles> selectUserRoles = userRolesmapper.selectUserRoles(selectuser.get(0).getId());
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		String gTime = sdf.format(date);
		String s = sdf1.format(date);
		String laiTime=s+" 06:00:00";
		String xTime=s+" 09:00:00";
		userchecks.setUserId(selectuser.get(0).getId());
		userchecks.setUserName(name);
		userchecks.setCheckInTime(gTime);
		userchecks.setCheckState(request.getParameter("checkState"));
		userchecks.setIsCancel("δǩ��");
		try {
			long time6 = sdf.parse(laiTime).getTime();
			long timeG = sdf.parse(gTime).getTime();
			long time8 = sdf.parse(xTime).getTime();
			if((timeG-time6)>0){
				if((time8-timeG)>0){
					k=userchecksmapper.addUsercheck(userchecks);
					if(k>0){
						if(selectUserRoles.get(0).getRoleId()!=1 && selectUserRoles.get(0).getRoleId()!=4){
							Askers askers = new Askers();
							askers.setAskerName(name);
							askers.setRoleName(selectRoles.get(0).getName());
							askers.setCheckState(request.getParameter("checkState"));
							askers.setCheckInTime(gTime);
							askers.setUserid(selectuser.get(0).getId());
							askersmapper.addAskersUsername(askers);
						}
					}
				}else{
					//上班迟到了
					Askers askers = new Askers();
					askers.setAskerName(name);
					askers.setRoleName(selectRoles.get(0).getName());
					askers.setCheckState(request.getParameter("checkState"));
					askers.setCheckInTime(gTime);
					askers.setUserid(selectuser.get(0).getId());
					askersmapper.addAskersUsername(askers);
					userchecksmapper.addUsercheck(userchecks);
						k=-1;
				}
			}else{
				
				k=3;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return k;
	}

	@Override
	public List<Userchecks> selectCheckUserJinTian(HttpServletRequest request) {
		Userchecks userchecks = new Userchecks();
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		Users u = new Users();
		u.setLoginName(name);
		List<Users> selectuser = usersService.selectByname(u);
		userchecks.setUserId(selectuser.get(0).getId());
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(date);
		userchecks.setCheckInTime(format);
		return userchecksmapper.selectCheckUserJinTian(userchecks);
	}

	@Override
	public Integer updateUserCheck(HttpServletRequest request) {
		Integer i = 0;
		Userchecks userchecks = new Userchecks();
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		Users u = new Users();
		u.setLoginName(name);
		/* 根据名称查询用户信息 */
		List<Users> selectuser = usersService.selectByname(u);
		Date date = new Date();

		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		String checkTime = sdf1.format(date);
		userchecks.setUserName(name);
		userchecks.setCheckInTime(checkTime);
		userchecks.setUserId(selectuser.get(0).getId());
		List<Userchecks> selectCheckUserJinTian = userchecksmapper
				.selectCheckUserJinTian(userchecks);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String CheckOutTime = sdf.format(date);
		String qdTime = checkTime + " 20:00:00";
		try {
			long time2 = sdf.parse(qdTime).getTime();
			long time1 = sdf.parse(CheckOutTime).getTime();
			if ((time1 - time2) > 0) {
				if (selectCheckUserJinTian.size() > 0) {
					String isCancel = request.getParameter("isCancel");
					userchecks.setCheckOutTime(CheckOutTime);
					userchecks.setIsCancel(isCancel);
					userchecks.setCheckInTime(selectCheckUserJinTian.get(0)
							.getCheckInTime());
					userchecksmapper.updateUserCheck(userchecks);
					i = 1;
				}
			} else {
				i = 2;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public Fenye<Userchecks> selectCheckUser(Fenye<Userchecks> fenye) {
		List<Userchecks> selectCheckUser = userchecksmapper.selectCheckUser(fenye);
		Integer selectCheckUserCount = userchecksmapper
				.selectCheckUserCount(fenye);
		fenye.setTotal(selectCheckUserCount);
		fenye.setRows(selectCheckUser);
		return fenye;
	}

	@Override
	public Integer updateUserCheckQC(Userchecks userchecks) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sdf.format(date);
		userchecks.setCheckOutTime(format);
		return userchecksmapper.updateUserCheckQC(userchecks);
	}
	@Autowired
	private Userchecks userchecks;
	@Override
	public Integer upsignon(String userId) {
		// TODO Auto-generated method stub
		String[] ids = userId.split(",");
		Integer j = 0;
		for(int i=0;i<ids.length;i++) {
			Integer user_ids = Integer.parseInt(ids[i]);
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = sdf.format(date);
			userchecks.setCheckOutTime(format);
			 j = userchecksmapper.upsignon(user_ids);
		}
		return j;
	}

}
